package com.douwong.student.adapter;

import android.os.AsyncTask;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.douwong.student.application.BaseApplication;
import com.douwong.student.contant.NetConfig;
import com.douwong.student.interfaces.WorkResultNotify;
import com.douwong.student.model.PrepareBean;
import com.douwong.student.model.PrepareResource;
import com.douwong.student.model.RequestAction;
import com.douwong.student.model.StatusCode;
import com.douwong.student.utils.LogUtils;
import com.douwong.student.utils.NetUtils;

import org.apache.http.message.BasicNameValuePair;

import java.util.ArrayList;
import java.util.List;

public class MicroListTask extends AsyncTask<String, Integer, Integer> {
	private static final String TAG ="MicroListTask" ;
	private BaseApplication app;
	private ArrayList<PrepareResource> listData = null;
	private WorkResultNotify<PrepareResource> workResult = null;
	public static int NO_DATA = 0;// 没有新的数据
	public static int REQUESTOK = 1;// 加载更多
	public static int NO_NETWOK = -1;// 没有网络
	public static int ERROR=2;
	private RequestAction action;
	private String key = null;
	public MicroListTask(RequestAction action, BaseApplication app) {
		this.app = app;
		this.action = action;
	}

	public void setWorkStautsListener(WorkResultNotify<PrepareResource> workResult) {
		this.workResult = workResult;
	}
	@Override
	protected Integer doInBackground(String... params) {
		int resultCode = REQUESTOK;
		key = BaseApplication.sUserBean.getUserbean().getEuNumber() + "_microlist_"
				+ params[0];
		if (RequestAction.INIT == action) {
			if (app.readObject(key) != null) {
				listData = (ArrayList<PrepareResource>) app.readObject(key);
				return resultCode;
			} else if (NetUtils.NetwrokIsUseful(app.mContext)) {
				resultCode = accessNetworkRequestData(params);
			} else {
				resultCode = NO_NETWOK;
			}
		} else if (NetUtils.NetwrokIsUseful(app.mContext)) {
			resultCode = accessNetworkRequestData(params);
		} else {
			resultCode = NO_NETWOK;
		}

		return resultCode;
	}
	@Override
	protected void onPostExecute(Integer result) {
		super.onPostExecute(result);
		workResult.notify(result, listData);
	}
	private int accessNetworkRequestData(String... params) {
		String response = null;
		int resultCode = REQUESTOK;
		List<BasicNameValuePair> arrayParams = new ArrayList<BasicNameValuePair>();
		Log.e("MicroListTask", "userId: "+ BaseApplication.sUserBean
				.getUserbean().getId() );
		Log.e("MicroListTask", "classId: "+ BaseApplication.sUserBean
				.getPersonalbean().getClassId() );
		arrayParams.add(new BasicNameValuePair("userId", BaseApplication.sUserBean
				.getUserbean().getId()+""));
		arrayParams.add(new BasicNameValuePair("classId", BaseApplication.sUserBean
				.getPersonalbean().getClassId()+""));
		arrayParams.add(new BasicNameValuePair("isPush", "1"));
		arrayParams.add(new BasicNameValuePair("rowsPerPage", "10"));
		arrayParams.add(new BasicNameValuePair("page", params[0]));
		try {
			response = NetUtils
					.httpGet(NetConfig.MICRO_LIST, arrayParams);
			Log.e("response", response + "");
			StatusCode<PrepareBean> sc = (StatusCode<PrepareBean>) JSON
					.parseObject(response,
							new TypeReference<StatusCode<PrepareBean>>() {
							});
			LogUtils.i(TAG,response);
			if (sc!=null&&sc.isFlag()) {
				Log.e("sc", sc.getData().getRows() + "");
				if ((sc.getData().getRows() == null
						|| sc.getData().getRows().equals(""))&& RequestAction.REFRESH == action)
						 {
					app.saveObject(listData, key);
					return NO_DATA;
				}if ((sc.getData().getRows() == null
						|| sc.getData().getRows().equals(""))&& RequestAction.LOADMROE == action)
						 {
					app.saveObject(listData, key);
					return NO_DATA;
				}
				listData = (ArrayList<PrepareResource>) JSON.parseArray(sc.getData()
						.getRows(), PrepareResource.class);
				if(action==RequestAction.REFRESH){
					app.saveObject(listData, key);
				}else if(action==RequestAction.INIT){
					app.saveObject(listData, key);
				}
			}else {
				return NO_DATA;
			}

		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
		return resultCode;
	}
}
